package com.medical.project.system.service.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.medical.common.utils.DateUtils;
import com.medical.common.utils.IdUtils;
import com.medical.project.system.domain.FamilyMember;
import com.medical.project.system.mapper.FamilyMemberMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.medical.project.system.mapper.FamilyArchivesMapper;
import com.medical.project.system.domain.FamilyArchives;
import com.medical.project.system.service.IFamilyArchivesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 家庭档案信息Service业务层处理
 *
 * @author tale
 * @date 2020-06-20
 */
@Service
public class FamilyArchivesServiceImpl implements IFamilyArchivesService {
    @Autowired
    private FamilyArchivesMapper familyArchivesMapper;
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 查询家庭档案信息
     *
     * @param familyCode 家庭档案信息ID
     * @return 家庭档案信息
     */
    @Override
    public FamilyArchives selectFamilyArchivesById(String familyCode) {
        return familyArchivesMapper.selectFamilyArchivesById(familyCode);
    }

    /**
     * 查询家庭档案信息列表
     *
     * @param familyArchives 家庭档案信息
     * @return 家庭档案信息
     */
    @Override
    public List<FamilyArchives> selectFamilyArchivesList(FamilyArchives familyArchives) {
        return familyArchivesMapper.selectFamilyArchivesList(familyArchives);
    }

    /**
     * 新增家庭档案信息
     *
     * @param familyArchives 家庭档案信息
     * @return 结果
     */
    @Override
    public int insertFamilyArchives(FamilyArchives familyArchives) {
        familyArchives.setCreateTime(DateUtils.getNowDate());
     /*   String uuid = IdUtils.fastSimpleUUID();
        String familyCode = familyArchives.getAreaCode() + uuid;*/

        String familyCode = familyArchives.getAreaCode() + System.currentTimeMillis();

        familyArchives.setFamilyCode(familyCode);
        boolean flag = saveBatch(familyArchives.getFamilyMembers(), familyCode);
        if (! flag) {

            return -1;
        }
        return familyArchivesMapper.insertFamilyArchives(familyArchives);
    }


    /**
     * 批量保存
     *
     * @param memberList
     * @param familyCode
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(Collection<FamilyMember> memberList, String familyCode) {
        try {
            for (Iterator member = memberList.iterator(); member.hasNext(); ) {
                FamilyMember anEntityList = (FamilyMember) member.next();
                anEntityList.setFamilyCode(familyCode);
                familyMemberMapper.insertFamilyMember(anEntityList);
            }
            return true;
        } catch (Throwable e) {
            throw e;
        }
    }

    /**
     * 批量更新
     *
     * @param memberList
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatch(Collection<FamilyMember> memberList){
        try {
            for (Iterator member = memberList.iterator(); member.hasNext(); ) {
                FamilyMember item = (FamilyMember) member.next();

                familyMemberMapper.updateFamilyMember(item);
            }
            return true;
        } catch (Throwable e) {
            throw e;
        }
    }

    /**
     * 修改家庭档案信息
     *
     * @param familyArchives 家庭档案信息
     * @return 结果
     */
    @Override
    public int updateFamilyArchives(FamilyArchives familyArchives) {

        boolean flag = updateBatch(familyArchives.getFamilyMembers());
        if (! flag) {

            return -1;
        }
        return familyArchivesMapper.updateFamilyArchives(familyArchives);
    }

    /**
     * 批量删除家庭档案信息
     *
     * @param familyCodes 需要删除的家庭档案信息ID
     * @return 结果
     */
    @Override
    public int deleteFamilyArchivesByIds(String[] familyCodes) {
        return familyArchivesMapper.deleteFamilyArchivesByIds(familyCodes);
    }

    /**
     * 删除家庭档案信息信息
     *
     * @param familyCode 家庭档案信息ID
     * @return 结果
     */
    @Override
    public int deleteFamilyArchivesById(String familyCode) {
        return familyArchivesMapper.deleteFamilyArchivesById(familyCode);
    }
}
